package com.spgf.controller.fragments;

import java.util.Calendar;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.archtecture.model.exceptions.NegocioException;
import com.spgf.controller.R;
import com.spgf.controller.utils.PadraoFragmentAb;
import com.spgf.model.entities.Categoria;
import com.spgf.model.entities.Lancamento;
import com.spgf.model.enums.FormaPagamento;
import com.spgf.model.enums.TipoCategoria;
import com.spgf.model.rn.CategoriaRN;
import com.spgf.model.rn.LancamentoRN;
import com.spgf.model.utils.AndroidUtil;

public class LancamentoCad extends PadraoFragmentAb {

	private EditText	edtData;
	private Spinner		spnCategoria;
	private Spinner		spnFormaPagamento;
	private EditText	edtValor;
	private EditText	edtObservacoes;

	private TextView	txData;
	private TextView	txCategoria;
	private TextView	txFormaPagamento;
	private TextView	txValor;

	protected Button	btnSalvar;

	@Override
	protected ViewGroup configurarViewRoot(final LayoutInflater inflater) {
		return (ViewGroup) inflater.inflate(R.layout.fragment_lancamento_cadastro, null);
	}

	@SuppressWarnings({"unchecked", "rawtypes"})
	@Override
	public void configurarTela(ViewGroup root) throws NegocioException {

		edtData = (EditText) root.findViewById(R.id.lanc_cad_edtData);
		edtData.setText(AndroidUtil.toString(Calendar.getInstance().getTime()));

		spnCategoria = (Spinner) root.findViewById(R.id.lanc_cad_spnCategoria);
		spnCategoria.setAdapter(new ArrayAdapter(root.getContext(), android.R.layout.simple_spinner_dropdown_item, popularComboCategoria(root)));
		spnCategoria.setSelection(0, true);
		spnCategoria.setOnItemSelectedListener(acaoSelecionar);

		spnFormaPagamento = (Spinner) root.findViewById(R.id.lanc_cad_spnFormaPagamento);
		spnFormaPagamento.setSelection(3, true);

		edtValor = (EditText) root.findViewById(R.id.lanc_cad_edtValor);

		edtObservacoes = (EditText) root.findViewById(R.id.lanc_cad_edtObservacoes);

		btnSalvar = (Button) root.findViewById(R.id.lanc_cad_btnSalvar);
		btnSalvar.setOnClickListener(acaoExecutar);

		txCategoria = (TextView) root.findViewById(R.id.lanc_cad_txtCategoria);
		txData = (TextView) root.findViewById(R.id.lanc_cad_txtData);
		txFormaPagamento = (TextView) root.findViewById(R.id.lanc_cad_txtFormaPagamento);
		txValor = (TextView) root.findViewById(R.id.lanc_cad_txtValor);
	}

	@Override
	protected void executarClick(final View pView) throws NegocioException {

		if (pView.getId() == R.id.lanc_cad_btnSalvar) {

			AndroidUtil.validarObjetosNulos(spnCategoria.getSelectedItem(), txCategoria.getText().toString());
			AndroidUtil.validarObjetosNulos(edtData.getText().toString(), txData.getText().toString());
			AndroidUtil.validarObjetosNulos(FormaPagamento.values()[spnFormaPagamento.getSelectedItemPosition()], txFormaPagamento.getText()
					.toString());
			AndroidUtil.validarObjetosNulos(edtValor.getText().toString(), txValor.getText().toString());

			final Lancamento lancamento = new Lancamento();
			lancamento.setData(AndroidUtil.toDate(edtData.getText().toString()));
			lancamento.setObservacao(edtObservacoes.getText().toString());
			lancamento.setValor(Double.valueOf(edtValor.getText().toString()));
			lancamento.setCategoria((Categoria) spnCategoria.getSelectedItem());
			lancamento.setFormaPagamento(FormaPagamento.values()[spnFormaPagamento.getSelectedItemPosition()]);

			new LancamentoRN(pView.getContext()).insert(lancamento);

			Toast.makeText(pView.getContext(), "Lancamento salvo com sucesso!", Toast.LENGTH_LONG).show();
		}
	}

	@Override
	protected void executarSelecao(AdapterView<?> pParent, View pView, int pPosition, long pID) throws NegocioException {

		super.executarSelecao(pParent, pView, pPosition, pID);

		if (pParent.getId() == R.id.lanc_cad_spnCategoria) {
			final Categoria categoria = new CategoriaRN(pView.getContext()).find(new Categoria(pParent.getItemAtPosition(pPosition).toString()));
			spnFormaPagamento.setEnabled(TipoCategoria.DEBITO.equals(categoria.getTipo()));
		}
	}

	private Object[] popularComboCategoria(final ViewGroup root) throws NegocioException {
		return new CategoriaRN(root.getContext()).findAll(new Categoria()).toArray();
	}
}
